import com.item.dao.model.User;
import io.swagger.annotations.ApiModelProperty;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;

public class JspDemo {

    public static void main(String[] args) {

        String filepath = "D:\\demo.jsp";
        String model = "com.item.dao.model";

        StringBuilder stringHtml = new StringBuilder();
        PrintStream printStream = null;

        User user = new User();
        String name = "user";
        Class<User> userClass = User.class;
        //ApiModelProperty userPro = userClass.getAnnotation(ApiModelProperty.class);


        Field[] fields = user.getClass().getDeclaredFields();

        ApiModelProperty c = fields[1].getAnnotation(ApiModelProperty.class);
        c.value();
        System.out.println(c.value());

        try{
            //打开文件
            printStream = new PrintStream(new FileOutputStream(filepath));
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
        stringHtml.append("<%@ page language=\"java\" contentType=\"text/html\"; charset=UTF-8\" pageEncoding=\"UTF-8\"%>"+"\r\n");
        stringHtml.append("<!DOCTYPE html>"+"\r\n");
        stringHtml.append("<html>"+"\r\n");
        stringHtml.append("<head>"+"\r\n");
        stringHtml.append("<%@ include file=\"static/admin/jsp/include.jsp\"%>"+"\r\n");
        stringHtml.append("<script type=\"text/javascript\">"+"\r\n");
        stringHtml.append("var grid;"+"\r\n");
        stringHtml.append("var index;"+"\r\n");
        stringHtml.append("var addFun = function($dialog, $grid, $pjq) {"+"\r\n");
        stringHtml.append("var dialog = parent.sy.modalDialog({"+"\r\n");
        stringHtml.append("title : '添加',"+"\r\n");
        stringHtml.append("width : 500,"+"\r\n");
        stringHtml.append("height : 400,"+"\r\n");
        stringHtml.append("url : sy.contextPath + '/go?path="+name+"/"+name+"+Edit',"+"\r\n");
        stringHtml.append("buttons : [ {"+"\r\n");
        stringHtml.append("text : '保存',"+"\r\n");
        stringHtml.append("handler : function() {"+"\r\n");
        stringHtml.append("dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);"+"\r\n");
        stringHtml.append("}"+"\r\n");
        stringHtml.append("} ]"+"\r\n");
        stringHtml.append("});"+"\r\n");
        stringHtml.append("};"+"\r\n");
        stringHtml.append("var editFun = function($dialog, $grid, $pjq) {");
        stringHtml.append("var rows = grid.datagrid('getSelections');");
        stringHtml.append("if (rows.length != 1) {");
        stringHtml.append("parent.$.messager.w('请选择一条记录进行编辑！');");
        stringHtml.append("return;");
        stringHtml.append("}");
        stringHtml.append("var dialog = parent.sy.modalDialog({");
        stringHtml.append("title : '修改资料',");
        stringHtml.append("width : 600,");
        stringHtml.append("height : 400,");
        stringHtml.append("url : sy.contextPath + '/go?path=user/userEdit&id=' + rows[0].id,");
        stringHtml.append("buttons : [ {");
        stringHtml.append("text : '保存',");
        stringHtml.append("handler : function() {");
        stringHtml.append("dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);");
        stringHtml.append("}");
        stringHtml.append("} ]");
        stringHtml.append("});");
        stringHtml.append("};");
        stringHtml.append("var creditsFun = function() {");
        stringHtml.append("var rows = grid.datagrid('getSelections');");
        stringHtml.append("if (rows.length != 1) {");
        stringHtml.append("parent.$.messager.w('请选择一条记录进行查看！');");
        stringHtml.append("return;");
        stringHtml.append("}");
        stringHtml.append("var nickName = \"\";");
        stringHtml.append("if (rows[0].nickName != null && rows[0].nickName != \"\") {");
        stringHtml.append("nickName = rows[0].nickName;");
        stringHtml.append("}");
        stringHtml.append("var url = sy.contextPath + '/go.do?path=user/userCreditLogList&id=' + rows[0].id;");
        stringHtml.append("var dialog = parent.sy.modalDialog({");
        stringHtml.append("title : '积分日志——' + nickName,");
        stringHtml.append("width:sy.width-150,");
        stringHtml.append("height:sy.height-100,");
        stringHtml.append("url : url");
        stringHtml.append("});");
        stringHtml.append("};");
        stringHtml.append("$(function() {");
        stringHtml.append("grid = $('#grid').datagrid({");
        stringHtml.append("url : sy.contextPath + '/"+name+"/list',");
        stringHtml.append("columns : [ [ ");

        for(int i = 0; i < fields.length; i++){
            stringHtml.append("{");
            stringHtml.append("width : $(this).width() * 0.15,");
            stringHtml.append("title : '"+fields[i].getAnnotation(ApiModelProperty.class).value()+"',");
            stringHtml.append("field : '"+fields[i].getName()+"',");
            stringHtml.append("align : 'center',");
            stringHtml.append("},");
        }
        stringHtml.append("] ]");
        stringHtml.append("});");
        stringHtml.append("</script>");
        stringHtml.append("</head>"+"\r\n");
        stringHtml.append("<body>"+"\r\n");
        stringHtml.append("<div id=\"toolbar\">");
        stringHtml.append("<form id=\"searchForm\">");
        stringHtml.append("<div>");
        stringHtml.append("<input type=\"text\" class=\"easyui-textbox\" name=\"name\" style=\"width: 150px\" prompt=\"请输入\"/>");
        stringHtml.append("<a href=\"javascript:void(0);\" class=\"easyui-linkbutton\" data-options=\"iconCls:'fa-search'\" onclick=\"grid.datagrid('load',sy.serializeObject($('#searchForm')));\">过滤</a> ");
        stringHtml.append("<a href=\"javascript:void(0);\" class=\"easyui-linkbutton\" data-options=\"iconCls:'fa-search-minus'\" onclick=\"$('#searchForm input').val('');grid.datagrid('load',{});\">重置过滤</a>");
        stringHtml.append("</div>");
        stringHtml.append("</form>");
        stringHtml.append("<div class=\"tbbutton\">");
        stringHtml.append("</div>");
        stringHtml.append("</div>");
        stringHtml.append("<table id=\"grid\" data-options=\"fit:true,border:false\"></table>");

        stringHtml.append("</body>"+"\r\n");
        stringHtml.append("</html>"+"\r\n");

        try{
            //将HTML文件内容写入文件中
            printStream.println(stringHtml.toString());
        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
